package com.prs.crm.action.crm;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.domain.crm.Configure;
import com.prs.crm.domain.crm.Customer;
import com.prs.crm.domain.crm.CustomerContract;
import com.prs.crm.domain.crm.CustomerProject;
import com.prs.crm.domain.crm.Equipment;
import com.prs.crm.domain.crm.Fault;
import com.prs.crm.domain.crm.FaultFeedback;
import com.prs.crm.domain.crm.ProjectContract;
import com.prs.crm.service.crm.ConfigureService;
import com.prs.crm.service.crm.CustomerProjectService;
import com.prs.crm.service.crm.EquipmentService;
import com.prs.crm.service.crm.FaultFeedbackService;
import com.prs.crm.service.crm.FaultService;
import com.prs.crm.service.crm.ProjectContractService;

public class FaultAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	
	private CustomerProject customerProject;
	private ProjectContract projectContract;
	private List<Configure> configures;
	private Fault fault;
	private FaultFeedback faultFeedback;
	private Integer[] projectIds;
	
	private PaginationSupport pagination;
	private Integer[] faultIds;
	private FaultService faultService;
	private ProjectContractService projectContractService;
	private ConfigureService configureService;
	private List<Equipment> equipments;
	private EquipmentService equipmentService;
	private List<FaultFeedback> feedbacks;
	private FaultFeedbackService faultFeedbackService;
	private CustomerProjectService customerProjectService;
	//设置明细
	private String[] sheName;  //设置名称
	private String[] damaged;  //损坏状况
	//客户反馈
	private String[] day; //反馈时间
	private String[] linkman;  //联系人
	private String[] back;  //反馈意见
	private String[] person;  //接受人
	
	@Action("listFault")
	public String listFault(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Fault.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getFaultService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("listFault2")
	public String listFault2(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Fault.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getFaultService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("listFault3")
	public String listFault3(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Fault.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getFaultService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("editConfigure")
	public String editConfigure() {
		if(this.projectIds!=null){
			this.customerProject=this.customerProjectService.get(projectIds[0]);
		}
		this.projectContract=this.projectContractService.getContractByProject(customerProject).get(0);
		//查询项目的配置单
		configures=this.configureService.getConfigureByPro(customerProject);
		return SUCCESS;
	}
	@Action("editFault")
	public String editFault() {
		if(faultIds!=null){
			this.fault=this.faultService.get(faultIds[0]);
		}
		configures=this.configureService.getConfigureByPro(fault.getCustomerProject());
		this.equipments=this.equipmentService.getEquipmentByFault(fault);
		this.feedbacks=this.faultFeedbackService.getFeedbackByFault(fault);
		return SUCCESS;
	}
	@Action("editFault2")
	public String editFault2() {
		if(faultIds!=null){
			this.fault=this.faultService.get(faultIds[0]);
		}
		configures=this.configureService.getConfigureByPro(fault.getCustomerProject());
		this.equipments=this.equipmentService.getEquipmentByFault(fault);
		
		return SUCCESS;
	}
	@Action("editFault3")
	public String editFault3() {
		if(faultIds!=null){
			this.fault=this.faultService.get(faultIds[0]);
		}
		configures=this.configureService.getConfigureByPro(fault.getCustomerProject());
		this.equipments=this.equipmentService.getEquipmentByFault(fault);
		this.feedbacks=this.faultFeedbackService.getFeedbackByFault(fault);
		return SUCCESS;
	}
	@Action(value = "saveFault", results = {
			@Result(name = "success", type = "redirect", location = "viewCustomerProject?customerProject=${customerProject.getId()}"),
			@Result(name = "error", type = "chain", location = "editConfigure")})
	public String saveFault(){
		this.faultService.save(fault);
		//设置明细
		if(this.sheName!=null){
			Equipment eq=null;
			for(int i=0;i<sheName.length;i++){
				if(sheName[i]==null || sheName[i].equals("")){
					this.addActionError("请将设置明细填写完");
					return ERROR;
				}else{
					eq=new Equipment();
					eq.setName(sheName[i]);
					eq.setDamaged(this.damaged[i]);
					eq.setFault(fault);
					this.equipmentService.save(eq);
				}
			}
		}
		customerProject.setRepairDay(new Date());
		this.customerProjectService.submit(customerProject);
		
		fault.setCustomerProject(customerProject);
		fault.setProjectContract(projectContract);
		if(sheName==null){
			fault.setState("等待维修");
		}else{
			fault.setState("等待更换部件送达");
		}
		this.faultService.submit(fault);
		return SUCCESS;
	}
	@Action(value = "editBack", results = {
			@Result(name = "success", type = "redirect", location = "listFault"),
			@Result(name = "error", type = "chain", location = "editFault")})
	public String editBack(){
		fault.setState("设备已到达，等待维修");
		this.faultService.submit(fault);
		return SUCCESS;
	}
	@Action(value = "editBack2", results = {
			@Result(name = "success", type = "redirect", location = "listFault2"),
			@Result(name = "error", type = "chain", location = "editFault2")})
	public String editBack2(){
		fault.setState("维修完毕");
		this.faultService.submit(fault);
		return SUCCESS;
	}
	@Action(value = "saveFeedback", results = {
			@Result(name = "success", type = "redirect", location = "listFault3"),
			@Result(name = "error", type = "chain", location = "editFault3")})
	public String saveFeedback() throws ParseException{
		fault.setState("已回访");
		this.faultService.submit(fault);
		if(day!=null){
			SimpleDateFormat  sdf=new SimpleDateFormat("yyyy-MM-dd");
			for(int i=0;i<day.length;i++){
				if(day[i]==null || day[i].equals("")){
					this.addActionError("请将客户反馈填写完");
					return ERROR;
				}
				this.faultFeedback.setDay(sdf.parse(day[i]));
				this.faultFeedback.setLinkman(linkman[i]);
				this.faultFeedback.setBack(back[i]);
				this.faultFeedback.setPerson(person[i]);
				this.faultFeedback.setFault(fault);
				this.faultFeedbackService.save(faultFeedback);
			}
		}
		return SUCCESS;
	}

	public CustomerProject getCustomerProject() {
		return customerProject;
	}

	public void setCustomerProject(CustomerProject customerProject) {
		this.customerProject = customerProject;
	}

	public ProjectContract getProjectContract() {
		return projectContract;
	}

	public void setProjectContract(ProjectContract projectContract) {
		this.projectContract = projectContract;
	}

	public List<Configure> getConfigures() {
		return configures;
	}

	public void setConfigures(List<Configure> configures) {
		this.configures = configures;
	}

	public Fault getFault() {
		return fault;
	}

	public void setFault(Fault fault) {
		this.fault = fault;
	}

	public ProjectContractService getProjectContractService() {
		return projectContractService;
	}

	public void setProjectContractService(
			ProjectContractService projectContractService) {
		this.projectContractService = projectContractService;
	}

	public ConfigureService getConfigureService() {
		return configureService;
	}

	public void setConfigureService(ConfigureService configureService) {
		this.configureService = configureService;
	}
	public FaultService getFaultService() {
		return faultService;
	}
	public void setFaultService(FaultService faultService) {
		this.faultService = faultService;
	}
	public String[] getSheName() {
		return sheName;
	}
	public void setSheName(String[] sheName) {
		this.sheName = sheName;
	}
	public String[] getDamaged() {
		return damaged;
	}
	public void setDamaged(String[] damaged) {
		this.damaged = damaged;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getFaultIds() {
		return faultIds;
	}

	public void setFaultIds(Integer[] faultIds) {
		this.faultIds = faultIds;
	}

	public List<Equipment> getEquipments() {
		return equipments;
	}

	public void setEquipments(List<Equipment> equipments) {
		this.equipments = equipments;
	}

	public EquipmentService getEquipmentService() {
		return equipmentService;
	}

	public void setEquipmentService(EquipmentService equipmentService) {
		this.equipmentService = equipmentService;
	}

	public List<FaultFeedback> getFeedbacks() {
		return feedbacks;
	}

	public void setFeedbacks(List<FaultFeedback> feedbacks) {
		this.feedbacks = feedbacks;
	}

	public FaultFeedbackService getFaultFeedbackService() {
		return faultFeedbackService;
	}

	public void setFaultFeedbackService(FaultFeedbackService faultFeedbackService) {
		this.faultFeedbackService = faultFeedbackService;
	}
	public FaultFeedback getFaultFeedback() {
		return faultFeedback;
	}
	public void setFaultFeedback(FaultFeedback faultFeedback) {
		this.faultFeedback = faultFeedback;
	}
	public String[] getDay() {
		return day;
	}
	public void setDay(String[] day) {
		this.day = day;
	}
	public String[] getLinkman() {
		return linkman;
	}
	public void setLinkman(String[] linkman) {
		this.linkman = linkman;
	}
	public String[] getBack() {
		return back;
	}
	public void setBack(String[] back) {
		this.back = back;
	}
	public String[] getPerson() {
		return person;
	}
	public void setPerson(String[] person) {
		this.person = person;
	}
	public CustomerProjectService getCustomerProjectService() {
		return customerProjectService;
	}
	public void setCustomerProjectService(
			CustomerProjectService customerProjectService) {
		this.customerProjectService = customerProjectService;
	}
	public Integer[] getProjectIds() {
		return projectIds;
	}
	public void setProjectIds(Integer[] projectIds) {
		this.projectIds = projectIds;
	}
	
}
